package com.tianqu.tms.www.user.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.entity.UserAuth;
import com.tianqu.tms.entity.UserClass;
import com.tianqu.tms.entity.UserRole;
import com.tianqu.tms.www.user.viewhelper.UserTopViewHelper;

/**
 * 用户添加结果页面
 * 
 * @author tianshufu
 * 
 */
public class UserCreationResultController extends TmsController {

	private Logger logger = Logger
			.getLogger(UserCreationResultController.class);

	private static final String USERDETAILID = "userDetailId";

	private static final String USERINFO = "userInfo";

	@Override
	public void execute(UserContext uc) throws Exception {

		logger.debug(this.getClass().getName() + "#execute was called.");

		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();

			UserTopViewHelper view = (UserTopViewHelper) uc
					.getAttribute("userInfo");

			// userprofile初始化 为了往数据表中天加操作者信息
			TmsUserProfile userprofile = uc.getUserProfile();

			// 往entity中插值
			User user = new User();

			user.setLoginId(view.getUserId());
			user.setPassword(view.getPassword());
			user.setRealName(view.getUserName());
			user.setUpdateUserId(userprofile.getUniqueId());
			user.setInsertUserId(userprofile.getUniqueId());

			UserClass code = UserClass.getInstance();
			// 通过itemID和itemValue从字典表中取出对应的code存入数据库中
			user.setUcId(String.valueOf(code.getUserClassByUserClassName(view
					.getUserType())));

			// 去数据库中查是否存在该用户，保证用户的唯一性
			if (user.findUserInfoByUserId(view.getUserId(), 0)) {
				user.saveUserInfo(user,1);

				// 根据userId查询出用户的主键ID
				// 向权限表中添加一天权限信息
				user = user.getUserProfileByLoginid(view.getUserId(),
						TmsConstants.BLANK);
				UserAuth auth = UserAuth.getInstance();

				// 根据userClass查询roleId
				UserRole role = UserRole.getInstance();
				List<UserRole> roleList = role.getRoleinfoByUcId(Integer
						.parseInt(user.getUcId()),1);

				if (roleList.size() > 0) {
					for (int i = 0; i < roleList.size(); i++) {
						UserRole entity = roleList.get(i);
						auth.insertUserAuthInfo(user.getId(), entity.getRole());
					}
				}
			}

			uc.setAttribute(USERDETAILID, user.getId());// 将ID传到下个Action

			logger.info("UserInputController中获取的用户主键ID：" + user.getId());
			uc.removeAttribute(USERINFO);// 将确认信息从uc中remove掉

			logger
					.debug(this.getClass().getName()
							+ "#doAction was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

}
